package pl.gwt.client.gui;

import java.util.Iterator;
import java.util.List;

import pl.gwt.client.ClientDetailsService;
import pl.gwt.client.ClientDetailsServiceAsync;
import pl.gwt.client.common.Client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;


public class RightPanelAddClient extends RightPanel {
	DockPanel workPanel = new DockPanel();
	VerticalPanel detailsPanel = new VerticalPanel();
	HorizontalPanel buttonsPanel = new HorizontalPanel();
	
	Button saveButton = new Button("Save");
	Button cancelButton = new Button("Cancel");
	
	//boolean exist = true;	
	//client details
	Label clientNameLabel = new Label("Name:");
	Label clientPhoneNoLabel = new Label("Phone number:");
	Label clientAddressLabel = new Label("Address:");
	Label clientCountryLabel = new Label("Country:");
	Label clientCityLabel = new Label("City:");
	Label clientStateLabel = new Label("State");
	
	//client data
	TextBox clientNameTextBox = new TextBox();
	TextBox clientPhoneNoTextBox = new TextBox();
	TextBox clientAddressTextBox = new TextBox();
	TextBox clientCountryTextBox = new TextBox();
	TextBox clientCityTextBox = new TextBox();
	TextBox clientStateTextBox = new TextBox();
		
	
	final ClientDetailsServiceAsync getClientDetailsService = (ClientDetailsServiceAsync) GWT
	.create(ClientDetailsService.class);
	
	public RightPanelAddClient(String clientName){		
		super("ADD NEW CLIENT");
		
		//initMenuBar();
		
		//this.clientName = clientName;
		//this.clientNameLabel.setText(clientName);
		//this.clientNameLabel.setStyleName("bold");
		//initDetails();
		//initProjectsTree();
		
		detailsPanel.add(clientNameLabel);
		detailsPanel.add(clientNameTextBox);		
		detailsPanel.add(clientPhoneNoLabel);
		detailsPanel.add(clientPhoneNoTextBox);
		detailsPanel.add(clientAddressLabel);
		detailsPanel.add(clientAddressTextBox);
		detailsPanel.add(clientCountryLabel);
		detailsPanel.add(clientCountryTextBox);
		detailsPanel.add(clientCityLabel);
		detailsPanel.add(clientCityTextBox);
		detailsPanel.add(clientStateLabel);
		detailsPanel.add(clientStateTextBox);
		
		buttonsPanel.add(saveButton);
		buttonsPanel.add(cancelButton);
		
		detailsPanel.add(buttonsPanel);		

		//workPanel.add(menu, DockPanel.NORTH);
		workPanel.add(detailsPanel, DockPanel.WEST);
		//workPanel.add(projectsTree, DockPanel.EAST);
		//workPanel.add(buttonsPanel);		//
		workPanel.setStyleName("gwt-DockPanel");		
		addInsidePanel(workPanel);
		
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {				
				addClientToDB();
			}
		});
		
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {				
				
			}
		});		
		
	}
	
	private void addClientToDB(){
		Client client = new Client();
		client.init(clientNameTextBox.getText(), clientPhoneNoTextBox.getText(), clientAddressTextBox.getText(),
				clientCountryTextBox.getText(), clientCityTextBox.getText(), null);
		
		
		ServiceDefTarget endPointClientDetails = (ServiceDefTarget) getClientDetailsService;
		endPointClientDetails.setServiceEntryPoint("pl.gwt.server");

		
		

		// get the Client
		AsyncCallback<String> callbackClient = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				//exist = false;
			}

			/*
			public void onSuccess(Client client) {
				if(client.isInit()){
					clientPhoneNoLabel.setText(client.getPhone_no());
					//TODO: czy state jest potrzebny?
					clientStateLabel.setText("Malopolskie");
					clientCityLabel.setText(client.getCity());
					clientCountryLabel.setText(client.getCountry());
					clientAddressLabel.setText(client.getAddress());
					clientPhoneNoLabel.setText(client.getPhone_no());
				}
				else{
					final DialogBox dialogBox = new DialogBox();
					dialogBox.setText("Remote Procedure Call");
					dialogBox.setAnimationEnabled(true);
					final Button closeButton = new Button("Close");
					// We can set the id of a widget by accessing its Element
					//closeButton.getElement().setId("closeButton");
					VerticalPanel dialogVPanel = new VerticalPanel();

					dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
					dialogVPanel.add(closeButton);
					
					dialogBox.setWidget(dialogVPanel);

					// Add a handler to close the DialogBox
					closeButton.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							dialogBox.hide();							
						}
					});
					
					dialogBox.setText("Wrong client name");				
					dialogBox.center();
					closeButton.setFocus(true);					
					
				}
					
			}
			*/

			public void onSuccess(String result){// TODO Auto-generated method stub
			}



		};
		getClientDetailsService.addClient(client, callbackClient);
		
		
	}
	
	
	
	/*
	private void initProjectsTree(){
		ServiceDefTarget endPointClientAddEdit = (ServiceDefTarget) getClientAddEditService;
		endPointClientAddEdit.setServiceEntryPoint("pl.gwt.server");
		
		AsyncCallback<List<String>> projectsCallback = new AsyncCallback<List<String>>(){

			public void onFailure(Throwable caught) {
				
			}

			public void onSuccess(List<String> result) {
				TreeItem projects = new TreeItem("Projects");
				
				projectsTree.addItem(projects);
				for(Iterator<String> iter = result.iterator();; iter.hasNext()){
					projects.addItem(iter.next());
				}				
			}
		};
		
		//getClientDetailsService.getProjects(clientName, projectsCallback);
	}
	*/
	
	/*
	private void initMenuBar(){
		menu.setAutoOpen(true);
	    menu.setWidth("500px");
	    menu.setAnimationEnabled(true);
	    
	    clientMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Client", clientMenu));
	    projectsMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Projects", projectsMenu));
	    calendarMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Calendar", calendarMenu));
	    
	    //add new client to DB
	    clientMenu.addItem("Add New Client", new Command(){
			public void execute() {

			}	
	    });
	    
	    //edit client details
	    clientMenu.addItem("Edit Current Client", new Command(){

			public void execute() {
				
			}
	    
	    });
	    
	    //remove client from DB
	    clientMenu.addItem("Remove Current Client", new Command(){

			public void execute() {
				
			}
	    	
	    });
	    
	    //create new project for current selected customer
	    projectsMenu.addItem("Create new project for current customer", new Command(){
			public void execute() {
				
			}
	    });
	    
	    //add new client task
	    calendarMenu.addItem("Add client task", new Command(){
			public void execute() {
				
			}
	    });
	    
	    //add establishment from last contact with a current selected client
	    calendarMenu.addItem("Add new contact details", new Command(){
			public void execute() {
				
			}	    	
	    });
	}
	*/
	
	/*
	private void initDetails() {
		ServiceDefTarget endPointClientDetails = (ServiceDefTarget) getClientDetailsService;
		endPointClientDetails.setServiceEntryPoint("pl.gwt.server");



		// get the Client
		AsyncCallback<Client> callbackClient = new AsyncCallback<Client>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(Client client) {
				if(client.isInit()){
					clientPhoneNoLabel.setText(client.getPhone_no());
					//TODO: czy state jest potrzebny?
					clientStateLabel.setText("Malopolskie");
					clientCityLabel.setText(client.getCity());
					clientCountryLabel.setText(client.getCountry());
					clientAddressLabel.setText(client.getAddress());
					clientPhoneNoLabel.setText(client.getPhone_no());
				}
				else{
					final DialogBox dialogBox = new DialogBox();
					dialogBox.setText("Remote Procedure Call");
					dialogBox.setAnimationEnabled(true);
					final Button closeButton = new Button("Close");
					// We can set the id of a widget by accessing its Element
					//closeButton.getElement().setId("closeButton");
					VerticalPanel dialogVPanel = new VerticalPanel();

					dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
					dialogVPanel.add(closeButton);
					
					dialogBox.setWidget(dialogVPanel);

					// Add a handler to close the DialogBox
					closeButton.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							dialogBox.hide();							
						}
					});
					
					dialogBox.setText("Wrong client name");				
					dialogBox.center();
					closeButton.setFocus(true);					
					
				}
					
			}
		};
		getClientDetailsService.getClient(clientName, callbackClient);
		
	}
	*/
}


